package com.hoshiicloud.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.goods.entity.goods.GoodsBrand;
import com.hoshiicloud.goods.param.goods.GoodsBrandAddParam;
import com.hoshiicloud.goods.param.goods.GoodsBrandQueryParam;
import com.hoshiicloud.goods.param.goods.GoodsBrandUpdateParam;
import com.hoshiicloud.goods.service.GoodsBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 商品品牌表 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-04
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/goods/goods-brand")
@Api(value = "商品品牌", tags = {"商品品牌"})
public class GoodsBrandController {

    @Autowired
    private GoodsBrandService goodsBrandService;

    /**
     * @param param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询商品品牌表
     */
    @ApiOperation(value = "分页查询商品品牌表")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid GoodsBrandQueryParam param) {
        try {
            QueryWrapper<GoodsBrand> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(param.getCompanyId() != null, GoodsBrand::getCompanyId, param.getCompanyId())
                    .eq(StringUtils.isNotBlank(param.getChineseName()), GoodsBrand::getChineseName,
                            param.getChineseName())
                    .eq(StringUtils.isNotBlank(param.getEnglishName()), GoodsBrand::getEnglishName,
                            param.getEnglishName())
                    .eq(param.getOrdered() != null, GoodsBrand::getOrdered, param.getOrdered())
                    .eq(StringUtils.isNotBlank(param.getLogo()), GoodsBrand::getLogo, param.getLogo())
                    .eq(StringUtils.isNotBlank(param.getTrademarkNo()), GoodsBrand::getTrademarkNo,
                            param.getTrademarkNo())
                    .eq(StringUtils.isNotBlank(param.getRegistrationNo()), GoodsBrand::getRegistrationNo,
                            param.getRegistrationNo())
                    .eq(param.getProducing() != null, GoodsBrand::getProducing, param.getProducing())
                    .eq(StringUtils.isNotBlank(param.getAddress()), GoodsBrand::getAddress, param.getAddress())
                    .eq(StringUtils.isNotBlank(param.getRegistrationApplyNo()), GoodsBrand::getRegistrationApplyNo,
                            param.getRegistrationApplyNo())
                    .eq(StringUtils.isNotBlank(param.getRegistrationOffer()), GoodsBrand::getRegistrationOffer,
                            param.getRegistrationOffer())
                    .eq(StringUtils.isNotBlank(param.getBelongTo()), GoodsBrand::getBelongTo, param.getBelongTo())
                    .eq(StringUtils.isNotBlank(param.getRemark()), GoodsBrand::getRemark, param.getRemark())
                    .eq(param.getStatus() != null, GoodsBrand::getStatus, param.getStatus())
                    .eq(param.getDeleted() != null, GoodsBrand::getDeleted, param.getDeleted());
            IPage<GoodsBrand> page = new Page<>(Long.parseLong(param.getPageNumber()),
                    Long.parseLong(param.getPageSize()));
            String key = "GoodsBrandQueryParam{" + param.toString() + "},Page{pageNumber=" + param.getPageNumber()
                    + ",pageSize=" + param.getPageSize() + "}";

            return Response.data(goodsBrandService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询商品品牌表异常", e);
            return Response.failed("分页查询商品品牌表异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询商品品牌表
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询商品品牌表")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(goodsBrandService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增商品品牌表")
    public Response save(@RequestBody @Valid GoodsBrandAddParam param) {

        // Bean数据转换
        GoodsBrand entity;
        try {
            entity = BeanUtils.covertType(param, GoodsBrand.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            throw new ServiceException("Bean转换异常");
        }
        return Response.data(goodsBrandService.save(entity));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改商品品牌表
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改商品品牌表")
    public Response update(@RequestBody @Valid GoodsBrandUpdateParam param) {
        // Bean数据转换
        GoodsBrand entity;
        try {
            entity = BeanUtils.covertType(param, GoodsBrand.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            throw new ServiceException("Bean转换异常");
        }
        return Response.data(goodsBrandService.updateById(entity));
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除商品品牌表
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除商品品牌表")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        return Response.data(goodsBrandService.deleteById(id));
    }

    /**
     * @Author:campchen
     * @Description: StringUtils.join(ids, " ") 为缓存中添加唯一KEY  如不合适可选择更好方法
     * @Date:上午 10:18 2019/5/11
     * @Param: ids
     * @return: com.hoshiicloud.common.rpc.Response
     */
    @ApiOperation(value = "根据id集合查询商品品牌表")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {

        if (CollectionUtils.isEmpty(ids)) {
            return Response.failed("id不能为空");
        }
        ids.sort(null);
        return Response.data(goodsBrandService.getByIds(ids, StringUtils.join(ids, "")));
    }

    /**
     * 根据类目id， 查询某个类目下的品牌信息
     */
    @GetMapping("/getBrandCategoryByCateId")
    public Response getBrandCategoryByCateId(@RequestParam Long cateId) {
        return Response.data(goodsBrandService.getGoodsBrandsByCateId(cateId));
    }

    /**
     * 得到所有的商品品牌
     *
     * @return Response
     */
    @GetMapping("/getAllGoodsBrandName")
    public Response<List<Map<String, Object>>> getAllGoodsBrand() {
        return Response.data(goodsBrandService.getAllGoodsBrand());
    }
}
